package com.clear.honghefinance.admin.controller;


import com.clear.honghefinance.biz.modules.system.member.service.IMemberRegisterService;
import com.clear.honghefinance.wechat.service.WechatMpEventService;
import com.clear.honghefinance.wechat.util.XmlMessageParseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 微信回调Controller
 */
@Api(tags = "微信模块")
@RestController
@RequestMapping("/wxEvent")
@Slf4j
public class WechatCallbackController {

    @Resource
    private WechatMpEventService wechatMpEventService;

    @Resource
    private IMemberRegisterService memberRegisterService;

    /**
     * 接收微信事件推送、接收微信公众号推送事件
     *
     * @param signature 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     * @param timestamp 时间戳
     * @param nonce     随机数
     * @param echostr   随机字符串
     * @return 验签成功返回echostr，否则返回空串【timestamp+token+nonce+密文(可选)】做SHA1摘要签名后与signature对比，一致则验证成功
     */
    @ApiOperation(value = "接收微信公众号推送事件")
    @GetMapping(value = "/receiveMpEvent")
    public String receiveMpEvent(@RequestParam("signature") String signature,
                                 @RequestParam("timestamp") String timestamp,
                                 @RequestParam("nonce") String nonce,
                                 @RequestParam("echostr") String echostr) {
        log.info("get验签请求参数，signature:{}, timestamp:{}, nonce:{}, echostr:{}",
                signature, timestamp, nonce, echostr);

        boolean flag = wechatMpEventService.receiveMpEvent(signature, timestamp, nonce);
        if (flag) {
            return echostr;
        }
        return "unknown";
    }

    /**
     * 接收微信事件推送、接收微信公众号推送事件
     * 扫带参数二维码关注：消息为：{Ticket=gQGz8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAya1A5Qms0a3ljS0gxeVVDUDFDY2cAAgSoGLNmAwQQDgAA, CreateTime=1723013341, EventKey=qrscene_ScanReg_wx347b3b2a761d0276_dcafeac6f9d941c5a96f97d69783e54c, Event=subscribe, ToUserName=gh_6e508ba809bc, FromUserName=oVGIu6_1zuQ4tfvCLrbj2WRraUTI, MsgType=event}
     * 扫带参数二维码（已关注后）：消息为：{Ticket=gQGz8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAya1A5Qms0a3ljS0gxeVVDUDFDY2cAAgSoGLNmAwQQDgAA, CreateTime=1723013447, EventKey=ScanReg_wx347b3b2a761d0276_dcafeac6f9d941c5a96f97d69783e54c, Event=SCAN, ToUserName=gh_6e508ba809bc, FromUserName=oVGIu6_1zuQ4tfvCLrbj2WRraUTI, MsgType=event}
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "接收微信公众号推送事件")
    @PostMapping(value = "/receiveMpEvent", produces = "application/xml;charset=UTF-8")
    public String receiveMpEvent(HttpServletRequest request) throws IOException {
        // 1、解析xml
        Map<String, String> messageMap = XmlMessageParseUtil.parseXml(request.getInputStream());
        log.info("接收到微信服务器发送的消息：" + messageMap);

        // 2、根据推送事件的不同进行分别处理
        wechatMpEventService.receiveMpEvent(messageMap);

        return "扫描关注你";
    }
}
